﻿using Language.Resource;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Repository.Interface.Sys;
using VisionCloud.Domain.UnitOfWork;

namespace VisionCloud.Service.Sys
{
    public class SysDeviceUserService
    {
        public IUnitOfWork UnitOfWork { get; set; }

        public ISysDeviceUserRepository SysDeviceUserRepository { get; set; }

        public UserService userService { get; set; }

        public SysDeviceService DeviceService { get; set; }

        public ILogger<SysDeviceUserService> logger { get; set; }

        /// <summary>
        /// 多语言资源类
        /// </summary>
        public IStringLocalizer<ServiceResource> ServiceLocalizer { get; set; }

        /// <summary>
        /// 设备和人是否已经绑定
        /// </summary>
        /// <param name="userGuid">用户Guid</param>
        /// <param name="deviceGuid">设备GUid</param>
        /// <returns></returns>
        public async Task<bool> IsUserBoundDevice(Guid userGuid, Guid deviceGuid)
        {
            return await SysDeviceUserRepository.AnyAsync(i => i.UserGuid == userGuid && i.DeviceGuid == deviceGuid);
        }

        /// <summary>
        /// 设备是否已经绑定
        /// </summary>
        /// <param name="encryptedDogNumber"></param>
        /// <param name="encryptedDogType"></param>
        /// <returns></returns>
        public bool IsDeviceBound(string encryptedDogNumber, string encryptedDogType)
        {
            return SysDeviceUserRepository.IsDeviceBound(encryptedDogNumber, encryptedDogType);
        }

        /// <summary>
        /// 设备和人是否已经绑定
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="encryptedDogNumber"></param>
        /// <param name="encryptedDogType"></param>
        /// <returns></returns>
        public bool IsUserBoundDevice(string userAccount, string encryptedDogNumber, string encryptedDogType)
        {
            return SysDeviceUserRepository.IsUserBoundDevice(userAccount, encryptedDogNumber, encryptedDogType);
        }

        /// <summary>
        /// 把设备绑定给用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ApiResult> DevcieSetToUser(RegisterDto request)
        {
            ApiResult rb = new ApiResult();
            var userM = await userService.FindUser(request.Account, request.Password);
            if (userM == null)
            {
                rb.Code = AjaxCode.Error;
                rb.Message = ServiceLocalizer["YB0324"].Value;
                return rb;
            }
            else
            {
                return await DevcieSetToUser(request, userM, true);
            }
        }

        /// <summary>
        /// 把设备绑定给用户
        /// </summary>
        /// <param name="request"></param>
        /// <param name="userM">新创建没有保存在数据库的对象</param>
        /// <param name="isSaveimmediately"></param>
        /// <returns></returns>
        public async Task<ApiResult> DevcieSetToUser(RegisterDto request, SysUserDto userM, bool isSaveimmediately)
        {
            ApiResult rb = new ApiResult();
            var deviceEntity = await DeviceService.GetDeviceByencryptedDog(request.EncryptedDogNumber, request.EncryptedDogType);
            if (!(deviceEntity != null))
            {
                rb.Code = AjaxCode.Error;
                rb.Message = ServiceLocalizer["YB0325"].Value;
                return rb;
            }
            else
            {
                if (IsDeviceBound(request.EncryptedDogNumber, request.EncryptedDogType))
                {
                    rb.Code = AjaxCode.Error;
                    rb.Message = ServiceLocalizer["YB0326"].Value;
                    return rb;
                }
                else
                {
                    if (await IsUserBoundDevice(userM.YunUserGuid, deviceEntity.DeviceGuid))
                    {
                        rb.Code = AjaxCode.Error;
                        rb.Message = ServiceLocalizer["YB0327"].Value;
                        return rb;
                    }
                    else
                    {
                        SysDeviceUserRepository.BoundDeviceToUse(userM.YunUserGuid, deviceEntity.DeviceGuid);
                        await DeviceService.SetDeviceActive(deviceEntity, userM.YunUserGuid);

                        logger.LogInformation($"【{OperatorProvider.Get().Name}】把设备({deviceEntity.DeviceNumber})分配给：{userM.YunUserGuid}");
                        //增加
                        if (isSaveimmediately)
                        {
                            await UnitOfWork.SaveChangesAsync();
                            rb.Code = AjaxCode.Success;
                            rb.Message = "";
                            rb.Result = deviceEntity.DeviceGuid;
                            return rb;
                        }
                    }
                }
            }
            return null;
        }
    }
}